/*
 * Copyright (c) 2025, Lucas Chollet <lucas.chollet@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/MemoryStream.h>
#include <LibGfx/ImageFormats/BilevelImage.h>
#include <LibGfx/ImageFormats/CCITTDecoder.h>
#include <LibGfx/ImageFormats/CCITTEncoder.h>
#include <LibTest/TestCase.h>

namespace {

void test_roundtrip(ReadonlyBytes encoded, u32 width, u32 height)
{
    auto decoded = TRY_OR_FAIL(Gfx::CCITT::decode_ccitt_group4(encoded, width, height));
    auto bilevel = TRY_OR_FAIL(Gfx::BilevelImage::create_from_byte_buffer(decoded, width, height));

    auto output_stream = AllocatingMemoryStream();
    TRY_OR_FAIL(Gfx::CCITT::Group4Encoder::encode(output_stream, TRY_OR_FAIL(bilevel->to_gfx_bitmap())));
    auto reencoded = TRY_OR_FAIL(output_stream.read_until_eof());

    EXPECT_EQ(encoded, reencoded.span());
}

}

// The round_trip_ccitt4_n test cases are generated from the three strips contained in Tests/LibGfx/test-inputs/tiff/ccitt4.tiff.

TEST_CASE(round_trip_ccitt4_1)
{
    auto encoded = to_array<u8>({
        // clang-format off
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x92, 0x80, 0x3F, 0x3B, 0x04, 0x23,
        0xB0, 0x2A, 0x46, 0xC0, 0xF0, 0xA7, 0x62, 0xC0, 0x87, 0x63, 0x01, 0x24, 0x98, 0x1E, 0x0C,
        0x10, 0x91, 0xDE, 0x06, 0x73, 0xB4, 0x06, 0xC8, 0x2E, 0x76, 0x6A, 0x05, 0xC9, 0xA0, 0x35,
        0xCA, 0x70, 0x3C, 0x0C, 0x26, 0x52, 0x83, 0x2C, 0x89, 0xB3, 0xA8, 0x6D, 0xD1, 0x06, 0xE2,
        0x0D, 0xB8, 0x95, 0x80, 0x2E, 0x84, 0x2C, 0x83, 0xA4, 0x78, 0x22, 0x0D, 0xCB, 0x48, 0x1B,
        0xEC, 0x6A, 0x44, 0x1B, 0xE4, 0x28, 0x36, 0x48, 0x6D, 0xEC, 0xA9, 0x82, 0xF0, 0x44, 0x33,
        0xAC, 0x10, 0x6A, 0xDA, 0xA8, 0xF8, 0x22, 0x04, 0x25, 0x10, 0xD9, 0xB3, 0x4F, 0x08, 0x14,
        0x9C, 0xB5, 0xF2, 0x28, 0x0C, 0x7F, 0x44, 0x1F, 0x7D, 0xFC, 0x17, 0x5D, 0x02, 0xFF, 0x0B,
        0xFD, 0x7A, 0xEB, 0xFF, 0xFF, 0xFA, 0x49, 0x75, 0xFF, 0x5F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFE, 0x92, 0x5F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xD2, 0x5F,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xAF, 0xFF, 0xBD, 0x7E, 0x97, 0xFF, 0xFF, 0xFF, 0xE4, 0x1E,
        0x5D, 0xFC, 0x86, 0x79, 0x0F, 0xFC, 0x83, 0x4E, 0xC9, 0x3F, 0x7F, 0xA2, 0x0A, 0xC5, 0x9F,
        0xC8, 0x12, 0xB, 0x13, 0xEC, 0x0, 0x40, 0x4,
        // clang-format on
    });
    test_roundtrip(encoded, 400, 128);
}

TEST_CASE(round_trip_ccitt4_2)
{
    auto encoded = to_array<u8>({
        // clang-format off
        0x2B, 0x06, 0x76, 0x39, 0x91, 0x30, 0x37, 0x22, 0xA6, 0x50, 0x17, 0xFE, 0x3F, 0xFB, 0xF2,
        0x65, 0x4B, 0xFA, 0x43, 0xFF, 0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x5F, 0xD7,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF5, 0xFF, 0xFF, 0xFF, 0xFF, 0xAF,
        0xFF, 0xFF, 0xFF, 0xFD, 0x7F, 0xFF, 0xFF, 0xD7, 0xFF, 0xFF, 0xF7, 0xFF, 0xFF, 0xBF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xEB, 0xFF, 0xFD, 0x7F, 0xFF, 0xFF, 0x7F, 0xF7, 0xFF, 0xED, 0xFF,
        0xFF, 0xFB, 0x7F, 0xFF, 0xFF, 0xFB, 0xD3, 0x5B, 0xF1, 0xFF, 0xDF, 0xDF, 0xFD, 0x3F, 0xFB,
        0x7E, 0xFD, 0x3F, 0xF6, 0xFF, 0xFE, 0xFE, 0xFF, 0xFE, 0xFD, 0xA6, 0xB8, 0x8F, 0xFB, 0xF6,
        0xA3, 0x96, 0xEA, 0xAA, 0x10, 0x7E, 0x9F, 0xA7, 0xE9, 0xD1, 0x21, 0xF4, 0xB2, 0x45, 0x49,
        0xB4, 0xF5, 0xF4, 0xAD, 0xBA, 0xBF, 0xAE, 0xEB, 0xBE, 0xBB, 0xFB, 0xEB, 0xE5, 0xB8, 0x12,
        0x16, 0xBA, 0x62, 0xDE, 0xFD, 0x71, 0x6F, 0xFF, 0xFF, 0xFF, 0xFF, 0x4B, 0xFA, 0x5E, 0x94,
        0xB7, 0x31, 0x2E, 0xD2, 0xD2, 0xDA, 0x08, 0x28, 0xAD, 0x2C, 0x28, 0x00, 0x80, 0x08,
        // clang-format on
    });
    test_roundtrip(encoded, 400, 128);
}

TEST_CASE(round_trip_ccitt4_3)
{
    auto encoded = to_array<u8>({ 0x26, 0xC6, 0xA7, 0x85, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x0, 0x40, 0x4 });
    test_roundtrip(encoded, 400, 44);
}

TEST_CASE(round_trip_sunset)
{
    auto encoded = to_array<u8>({
        // clang-format off
        0x26, 0xA1, 0xB4, 0x47, 0x03, 0x60, 0x68, 0x23, 0x86, 0x41, 0x91, 0xC3, 0x24, 0x52, 0x38,
        0x1E, 0x0C, 0xB1, 0x20, 0x6C, 0x63, 0x89, 0x03, 0xC1, 0x90, 0x71, 0x12, 0x07, 0x82, 0x81,
        0xC8, 0x51, 0xF2, 0x0D, 0x83, 0x88, 0x90, 0x5B, 0x02, 0xE3, 0x89, 0x43, 0x8E, 0x71, 0xC8,
        0xC7, 0x12, 0x1A, 0x07, 0x20, 0x4D, 0x06, 0x91, 0xC8, 0x60, 0x72, 0x0C, 0x0E, 0x61, 0xC4,
        0x90, 0xE5, 0x8E, 0x61, 0xCB, 0x1C, 0x49, 0x8E, 0x48, 0x73, 0x0E, 0x71, 0xC2, 0x58, 0x89,
        0x43, 0x90, 0x6A, 0x1C, 0x48, 0x1B, 0x01, 0x90, 0x72, 0x07, 0x86, 0xA0, 0xE4, 0x32, 0x87,
        0x20, 0x7E, 0x39, 0x02, 0x81, 0xC8, 0x65, 0x0E, 0x58, 0xE6, 0x1C, 0x98, 0xE5, 0x8E, 0x25,
        0x8E, 0x61, 0xC4, 0xB1, 0xCB, 0x1C, 0x10, 0x21, 0x13, 0x8E, 0x4C, 0x71, 0x11, 0x13, 0x0E,
        0x27, 0x1C, 0x44, 0x46, 0x00, 0x20, 0x02,
        // clang-format on
    });
    test_roundtrip(encoded, 1024, 5);
}

TEST_CASE(round_trip_jbig2_crash)
{
    auto encoded = to_array<u8>({
        // clang-format off
        0x23, 0xe5, 0xf2, 0xf8, 0x25, 0x00, 0x10, 0x01
        // clang-format on
    });
    test_roundtrip(encoded, 16, 1);
}
